__author__ = "F.di Dio"
__version__ = "1.5.8"
__email__ = "tabuto83@gmail.com"
__status__ = "Test"


import imaplib
import time
import datetime
import email
from email.parser import HeaderParser
from optparse import OptionParser
import os




#cartella dove salva le mail
LOCAL_FOLDER="/home/didio/Script/mails/"
CONF_FILE="/home/didio/Script/pymap.conf"
#SPEED_TEST_FOLDER="INBOX/test"


def log(message, show):
	if show:
		print message;

def getDateTime():
	utc_datetime = datetime.datetime.utcnow();
	now_timestamp = time.time()
	offset = datetime.datetime.fromtimestamp(now_timestamp) - datetime.datetime.utcfromtimestamp(now_timestamp)
	r = utc_datetime + offset
	return str(r)


	

'''
Return a dictionary of dictionaries contains the mailbox read from the pymap.conf
'''
def loadConf(filename):
	caselle = {}
	with open(filename) as f:
		content = f.readlines()
	f.close()
	global LOCAL_FOLDER
	LOCAL_FOLDER = content.pop(0).split("=")[1].replace('"','').rstrip()
	
	
	for line in content:
		#gestione commenti
		if line[0]=='#':
			continue;
		start = line.find(':')
		keyname = line[:start].strip()
		attributes=line[start+1:].split(',')
		box={}
		for att in attributes:
			attList = att.replace('{', '').replace("}", "").replace("'", "").split(':')
			key=attList[0].strip()
			value=attList[1].strip()
			box[key]=value
		caselle[keyname]=box
		
	return caselle

#Arc caselle mail

#casella da testare
mailboxname = ''


checkAll=False
showList=False
countAll=False
showLog=False






#mostra la lista delle caselle mail
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("-s", "--showBox",action="store_true", dest="showList",help="show the mailbox list")
parser.add_option("-c", "--checkAll",action="store_true", dest="checkAll", help="download one mail from all mailboxes ")
parser.add_option("-n", "--countAll",action="store_true", dest="countAll", help="count email in the FOLDER for all mailboxes")
parser.add_option("-b", "--mailbox",action="store", type="string", dest="mailboxname",help="download all mails from selected mailbox")
parser.add_option("-v", "--verbose",action="store_true", dest="showLog",help="show verbose log")


#parser.add_option("-t", "--host",action="store", type="string", dest="IMAP_HOST",help="set the imap host")
#parser.add_option("-f", "--folder",action="store", type="string", dest="LOCAL_FOLDER",help="set local mail store folder")


(options, args) = parser.parse_args();

checkAll=options.checkAll
showList=options.showList
countAll=options.countAll
showLog=options.showLog


log( "\n\nStart Pymap v."+__version__,showLog)
log("Date: " + str(datetime.datetime.utcnow()),showLog)
caselle = loadConf(CONF_FILE);
log( "Configuration File: "+CONF_FILE+" succesfully loaded!",showLog)

if options.mailboxname:
	mailboxname=options.mailboxname
#if options.IMAP_HOST:
	#IMAP_HOST = options.IMAP_HOST
#if options.LOCAL_FOLDER:
#	LOCAL_FOLDER = options.LOCAL_FOLDER; 



#SHOW MAIL BOX LIST
if showList:
	print getDateTime()
	print 'Mail Folder: '+LOCAL_FOLDER
	print 'Show Mail Box List'
	for key in caselle:
		print str(key) +' : '+ str(caselle[key]);




#CHECK STATO CASELLE
if checkAll:
	log( 'CheckAll enabled: check all mail account',showLog)
	print "Time;BoxName;Size(bytes);time(s);Speed(Kb/s);Subject"
	for key in  caselle:
                allbytes = 0
                start_time_all = time.time();
		log( '\nCheck account: '+key,showLog)
		startCheck=time.time();
		#mail = imaplib.IMAP4_SSL(IMAP_HOST)
		
		account=caselle[key]
		mail = imaplib.IMAP4_SSL(account['MAIL_HOST'])
		mail.login(account['USER'], account['PSWD'])
		mail.list()
		mail.select(account['SPEED_TEST_FOLDER']) # connect to test_speed_folder
		typ, data = mail.search(None, 'ALL')
		for num in data[0].split():
			start_time = time.time();
			typ, data = mail.fetch(num, '(RFC822)')
			end_time = time.time();
			diff = end_time - start_time;
			f = open( os.path.join(LOCAL_FOLDER, str(num)+'.eml'), 'w')
			print >> f, data[0][1]
			varSubject=''
			#retrieve subject
			msg = email.message_from_string(data[0][1])
			varSubject = msg['subject']
			if (varSubject and len(varSubject) > 45):
					varSubject=varSubject[:45]


			print getDateTime() +';'+key+';'+str( len(data[0][1])) + ';'+ str(diff)+';'+ str( (len(data[0][1])/1024 )/diff ) +';'+str(varSubject)
			allbytes+=len(data[0][1])

		#Statistiche per l'intera casella
		end_time_all = time.time();
		diffAll = end_time_all - start_time_all;
		log( '>>>MailBox: '+key+' Read Kbytes: '+ str( (allbytes/1024) )+' in '+str(diffAll)+' s speed: '+ str( ((allbytes/1024)/diffAll)) +'Kb/s',showLog)

		mail.close()
		log( "Close...",showLog)
		mail.logout()
		log( "Logout: " + account['USER'],showLog)

		log( "Done!",showLog)

                                
		endCheck=time.time();
		log( 'MailBox checked in: ' + str(endCheck-startCheck)+' seconds',showLog)
        log ('End CheckAll routine\n\n',showLog)


#CHECK NUMERO MAIL PRESENTI IN DA IMPORTARE PER TUTTE LE CASELLE
if countAll:
	log( 'CountAll enabled: check all mail account message number',showLog)
	for key in  caselle:
                allbytes = 0
                start_time_all = time.time();
		log( '\nCheck account: '+key,showLog)
		startCheck=time.time();
		#mail = imaplib.IMAP4_SSL(IMAP_HOST)
		
		account=caselle[key]
		mail = imaplib.IMAP4_SSL(account['MAIL_HOST'])
		mail.login(account['USER'], account['PSWD'])
		mail.list()
		mail.select(account['FOLDER']) # connect to FOLDER
		typ, data = mail.search(None, 'ALL')

		#Statistiche per l'intera casella
		end_time_all = time.time();
		diffAll = end_time_all - start_time_all;
		print '>>>MailBox: '+key+': Da Importare in DocWeb( '+ str( len(data[0].split()) )+') '

		mail.close()
		log( "Closed!",showLog)
		mail.logout()
		log( "Logout: " + account['USER'],showLog)
		endCheck=time.time();
		log('MailBox checked in: ' + str(endCheck-startCheck)+' seconds',showLog)
        log( 'End CheckAll routine\n\n',showLog)


#DOWNLOAD DALLA CASELLA SELEZIONATA
if len(mailboxname) < 2:
	exit();

selected = caselle[mailboxname]
log( 'Process mailbox: '+mailboxname,showLog)
log( "try connect host: "+ selected['MAIL_HOST'],showLog)
mail = imaplib.IMAP4_SSL(selected['MAIL_HOST'])
#CREDENZIALI
log("login: " + selected['USER'],showLog)
mail.login(selected['USER'], selected['PSWD'])
log( "Connect to folder: " + selected['FOLDER'],showLog)
mail.list()
#Seleziona la cartella
mail.select(selected['FOLDER']) # connect to inbox.
#Recupera i riferimenti all'interno della cartella selezionata
typ, data = mail.search(None, 'ALL')

#Scorre i riferimenti e per ognuno scarica l'intera mail salvandola nel percorso selezionato
start_time_all = time.time();
allbytes=0
read_mail = 0;
print "Time;BoxName;Size(bytes);time(s);Speed(Kb/s);Subject"
for num in data[0].split():
	start_time = time.time();
	typ, data = mail.fetch(num, '(RFC822)')
	end_time = time.time();
	diff = end_time - start_time;
	f = open('%s/%s.eml' %(LOCAL_FOLDER, num), 'w')
	print >> f, data[0][1]
	varSubject=''
	#retrieve subject
	msg = email.message_from_string(data[0][1])
	varSubject = msg['subject']
	if len(varSubject) > 45:
		varSubject=varSubject[:45]
	print getDateTime() +';'+mailboxname+';'+str( len(data[0][1])) + ';'+ str(diff)+';'+ str( (len(data[0][1])/1024 )/diff ) +';'+varSubject
	allbytes+=len(data[0][1])
	read_mail=num

#Statistiche per l'intera casella
end_time_all = time.time();
diffAll = end_time_all - start_time_all;
log( '>>>MailBox: '+mailboxname+' Read '+str(read_mail)+' mails;'+' Read Kbytes: '+ str( (allbytes/1024) )+' in '+str(diffAll)+' s ( average speed: '+ str( ((allbytes/1024)/diffAll)) +'Kb/s)',showLog)
mail.close()
log( "Close...",showLog)
mail.logout()
log( "Logout: " + selected['USER'],showLog)
log( "Done!",showLog)

